package com.hengli.controller.admin;

import com.hengli.dto.WineDTO;
import com.hengli.dto.WinePageQueryDTO;
import com.hengli.entity.Category;
import com.hengli.entity.Wine;
import com.hengli.result.PageResult;
import com.hengli.result.Result;
import com.hengli.service.WineService;
import com.hengli.vo.WineVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 酒品管理
 * @author ljx
 * @date 2024/3/24 15:21
 */
@RestController
@RequestMapping("admin/wine")
//属于 Swagger 规范的一部分
@Api(tags = "酒品管理")
@Slf4j
@CrossOrigin
public class WineController {

    @Autowired
    private WineService wineService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 添加酒品
     * @param wineDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增酒品")
    public Result save(@RequestBody WineDTO wineDTO) {
        log.info("新增酒品：{}", wineDTO);

        wineService.saveWithSpecification(wineDTO);
        // 清理缓存数据
        String key = "wine_" + wineDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 酒品分页查询
     *
     * @param winePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("酒品分页查询")
    public Result<PageResult> page(WinePageQueryDTO winePageQueryDTO) {
        log.info("酒品分页查询:{}", winePageQueryDTO);
        PageResult pageResult = wineService.pageQuery(winePageQueryDTO);//后绪步骤定义
        return Result.success(pageResult);
    }

    /**
     * 酒品批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("酒品批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("酒品批量删除：{}", ids);
        wineService.deleteBatch(ids);//后绪步骤实现

        // 将所有的酒品缓存数据清理掉，所有以wine_开头的key
        cleanCache("wine_*");
        return Result.success();
    }

    /**
     * 根据id查询酒品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询酒品")
    public Result<WineVO> getById(@PathVariable Long id) {
        log.info("根据id查询酒品：{}", id);
        WineVO wineVO = wineService.getByIdWithSpecification(id);//后绪步骤实现
        return Result.success(wineVO);
    }

    /**
     * 修改酒品
     *
     * @param wineDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改酒品")
    public Result update(@RequestBody WineDTO wineDTO) {
        log.info("修改酒品：{}", wineDTO);
        wineService.updateWithSpecification(wineDTO);

        // 将所有的酒品缓存数据清理掉，所有以wine_开头的key
        cleanCache("wine_*");
        return Result.success();
    }

    /**
     * 酒品起售停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("酒品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status,@RequestBody Long id) {
        wineService.startOrStop(status, id);
        // 将所有的酒品缓存数据清理掉，所有以wine_开头的key
        cleanCache("wine_*");
        return Result.success();
    }

    /**
     * 根据id查询酒品
     * @param id
     * @return
     */
    @GetMapping("/list/{id}")
    @ApiOperation("根据类型查询酒品")
    public Result<List<Wine>> list(@PathVariable Integer id){
        log.info("根据类型查询酒品：{}", id);
        List<Wine> list = wineService.list(id);
        return Result.success(list);
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
